﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FlipPanel
{
    [TemplateVisualState(Name = "Front", GroupName = "VisualStates")]
    [TemplateVisualState(Name = "Back", GroupName = "VisualStates")]
    [TemplatePart(Name = "FlipButton", Type = typeof(ToggleButton))]
    [TemplatePart(Name = "AlternateFlipButton", Type = typeof(ToggleButton))]
    public class FlipPanel : Control
    {
        public static readonly DependencyProperty FrontContentProperty =
            DependencyProperty.Register("FrontContent", typeof (object), typeof (FlipPanel), new PropertyMetadata(default(object)));

        public object FrontContent
        {
            get { return (object) GetValue(FrontContentProperty); }
            set { SetValue(FrontContentProperty, value); }
        }

        public static readonly DependencyProperty BackContentProperty =
            DependencyProperty.Register("BackContent", typeof (object), typeof (FlipPanel), new PropertyMetadata(default(object)));

        public object BackContent
        {
            get { return (object) GetValue(BackContentProperty); }
            set { SetValue(BackContentProperty, value); }
        }

        public static readonly DependencyProperty IsFlippedProperty =
            DependencyProperty.Register("IsFlipped", typeof (bool), typeof (FlipPanel), new PropertyMetadata(OnIsFlippedChanged));

        private static void OnIsFlippedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var panel = (FlipPanel) dependencyObject;
            if (panel.IsFlipped)
            {
                VisualStateManager.GoToState((Control)dependencyObject, "Back", true);
                return;
            }

            VisualStateManager.GoToState((Control) dependencyObject, "Front", true);
        }

        public bool IsFlipped
        {
            get { return (bool) GetValue(IsFlippedProperty); }
            set { SetValue(IsFlippedProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof (CornerRadius), typeof (FlipPanel), new PropertyMetadata(default(CornerRadius)));

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius) GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public FlipPanel()
        {
            DefaultStyleKey = typeof (FlipPanel);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var flipButton = (ToggleButton)GetTemplateChild("FlipButton");
            flipButton.Click += FlipButtonOnClick;

            VisualStateManager.GoToState(this, "Front", false);
        }

        private void FlipButtonOnClick(object sender, RoutedEventArgs routedEventArgs)
        {
            IsFlipped = !IsFlipped;
        }
    }
}
